package controller;

import convert.myconvert;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import pojo.User;
import service.AdminService;
import service.impl.AdminServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Map;

/**
 * @author LiangHaiBo
 * @version 1.0
 * @date 2021.8.31 下午 20:03
 */
@WebServlet("/userupdate")
public class UserUpdateServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        /*request.setCharacterEncoding("utf-8");//解决post提交中文乱码
        response.setContentType("text/html;charset=utf-8");//响应乱码解决*/

        User user = new User();

        Map<String, String[]> parameterMap = request.getParameterMap();

        ConvertUtils.register(new myconvert(), Date.class);

        try {
            BeanUtils.populate(user,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        //调用业务接口
        AdminService adminService = new AdminServiceimpl();
        adminService.updateget(user);

        request.getRequestDispatcher("/findUserByPage").forward(request,response);

    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}
